/**
 * @copyright SilverBiology, LLC
 * @author Michael Giddens
 * @website http://www.silverbiology.com
*/

Ext.namespace('Taxamatch');
Taxamatch.Authorities = function(config) {

	this.ds = new Ext.data.JsonStore({
			proxy: new Ext.data.HttpProxy({
					url: 'resources/api/app.php'
			})
		,	baseParams: { 
					cmd: 'getAuthorityList'
			}
		,	root: 'data'
		,	fields: [
					{name: 'active'}
				,	{name: 'id'}	
				,	{name: 'name'}
				,	{name: 'created'}
				,	{name: 'lastUpdated'}
				,	{name: 'kingdom'}
				,	{name: 'phylum'}	
				,	{name: 'class'}
				,	{name: 'order'}
				,	{name: 'family'}
				,	{name: 'genus'}
				,	{name: 'species'}
				,	{name: 'database'}	
			]
		,	remoteSort: false
	});
	
	this.activeColumn = new Ext.grid.CheckColumn({
			width: 40
		,	header: 'Active'
		,	dataIndex: 'active'
		,	sortable: true
	});

	this.activeColumn.on('cellclick', this.sendData, this);
	
	Ext.apply(this, config, {
			title: 'Authorities'	
		,	enableColumnMove: false
		,	enableColumnHide: false
		,	store: this.ds
		,	cls: 'no-dirty-mark'
		,	columns: [this.activeColumn, {
					header: "Name"
				,	dataIndex: 'name'
				,	width: 80
				,	sortable: true
			},{
					header: "Created"
				,	dataIndex: 'created'
				,	width: 80
				,	sortable: true
			},{
					header: "Last Updated"
				,	dataIndex: 'lastUpdated'
				,	width: 80
				,	scope:this
				,	sortable: true
			},{
					header: "Kingdom"
				,	dataIndex: 'kingdom'
				,	width: 80
				,	scope:this
				,	sortable: true
			},{
					header: "Phylum"
				,	dataIndex: 'phylum'
				,	width: 80
				,	scope:this
				,	sortable: true
			},{
					header: "Class"
				,	dataIndex: 'class'
				,	width: 80
				,	scope:this				
				,	sortable: true
			},{
					header: "Order"
				,	dataIndex: 'order'
				,	width: 80
				,	sortable: true
			},{
					header: "Family"
				,	dataIndex: 'family'
				,	width: 80
				,	sortable: true
			},{
					header: "Genus"
				,	dataIndex: 'genus'
				,	width: 80
				,	scope:this
				,	sortable: true
			},{
					header: "Species"
				,	dataIndex: 'species'
				,	width: 80
				,	scope:this
				,	sortable: true
			}]
		,	sm: new Ext.grid.RowSelectionModel({singleSelect: true})
		,	loadMask: true
		,	plugins: [this.activeColumn]
		,	tbar:[{
					text:'Delete Authority'
				,	handler:this.deleteAuthority
				,	iconCls:'icon-databaseDelete'
				,	scope:this
			},{
					text:'Test Authority'
				,	handler:this.testAuthority
				,	iconCls:'icon-databaseTest'
				,	scope:this
			},{
					text:'Browse Authority'
				,	handler:this.browseAuthority
				,	iconCls:'icon-databaseBrowse'
				,	scope:this
			}]
		,	listeners: {
				rowcontextmenu: this.showContextMenu
			}	
	})

	Taxamatch.Authorities.superclass.constructor.call(this, config);

};

Ext.extend(Taxamatch.Authorities, Ext.grid.GridPanel, {
		
		browseAuthority: function(){
			if(this.getSelectionModel().getSelected()){
				var record = this.getSelectionModel().getSelected().data;
				var ba = new Taxamatch.BrowseAuthorities();
				ba.setTitle(record.name);
				ba.show();
				ba.items.items[0].store.on('beforeload', function(s) {
					s.setBaseParam('database', record.database);
					s.setBaseParam('source', record.id);
				});
				ba.items.items[0].store.load();
			} else {
				Taxamatch.Notice.msg('Notice', 'Authority must be selected.');
			}	
		}

	,	showContextMenu: function(grid, row, e){
			grid.getSelectionModel().selectRow(row);
			var record = grid.getSelectionModel().getSelected().data;
			var items = [];
			items.push({
					text: 'Test Authority'
				,	scope: this	
				,	handler:this.testAuthority
			},{
					text:  'Browse Authority'
				,	scope: this	
				,	handler:this.browseAuthority 	
			},{
					text: 'Export Taxamatch Archive'
				,	scope: this	
				,	handler: function() { 
						var d = Ext.get("download");
						if (!Ext.isEmpty(d)) {
							var params = {
									cmd: "exportTM"  
								,	source: record.name
								,	database: record.database
							};
							params = Ext.apply({}, this.store.lastOptions.params, params);
							Taxamatch.Notice.msg('Success','Please wait...');
							d.dom.src = Config.baseUrl + 'resources/api/app.php?' + Ext.urlEncode(params);
						} else {
							Taxamatch.Notice.msg("Error", "Error in downloading...");
						}
					}  	
			});
			var menu = new Ext.menu.Menu({
				items: items
			});
			var xy = e.getXY();
			menu.showAt(xy);
		}

	,	testAuthority: function(){
			if(this.getSelectionModel().getSelected()){
				var record = this.getSelectionModel().getSelected().data;
				var ta = new Taxamatch.TestAuthorities();
				ta.store.baseParams = Ext.apply(ta.store.baseParams, {
						database: record.database
					,	source: record.id
				});				
				ta.show();
				ta.setTitle(record.name);
			} else {
				Taxamatch.Notice.msg('Notice','Authority must be selected.');		
			}	
		}
		
	,	deleteAuthority: function(){
			if(this.getSelectionModel().getSelected()){
				var record = this.getSelectionModel().getSelected().data;
				Ext.Msg.confirm('Notice', 'Do you really want to permanently remove \n this authority: ' + record.name + '?',function(btn){
					if(btn=='yes'){
						this.sendDelRequest(record);
					}
				}, this);
			} else {
				Taxamatch.Notice.msg('Notice','Authority must be selected.');		
			}	
		}

	,	sendDelRequest: function(record){
			Ext.Ajax.request({
					url: 'resources/api/app.php'
				,	params: { 
							cmd: 'deleteAuthority'
						, data: record.name
						,	database: record.database
					}												
				,	scope: this
				,	success: function(r) {
						this.getStore().reload();
						Taxamatch.Notice.msg('Notice','Authority deleted successfully');
					}
				,	failure: function(res) {
					}		
			}, this);
		}

	,	sendData: function(e, index, TCIO, rowData){
			if (TCIO != -1) {
				var active = (rowData.data.active) ? 1 : 0;	
				Ext.Ajax.request({
						url: 'resources/api/app.php'
					,	params: {
								cmd: 'activeAuthority'
							,	active: active
							,	id: rowData.data.id		
						}
					,	scope: this
					,	success: function(r){
							console.log("Success", r);
						}
					,	failure: function(r){
							console.log("Success", r);
						}
				})
			}
		}

});